package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.gdit.zax.Constant;
import com.gdit.zax.dao.CommentDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.CommentAll;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class GetCourseComment extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = TextUtil.String2int(request.getParameter("id"), -1);
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		
		//检测参数
		if (id<0) {
			O.printFail(response, 1, "请检查id参数!");
		}
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		CommentDao cdao = dao.getCommentDao();
		UserDao udao = dao.getUserDao();
//		ArrayList<Comment> list = cdao.listComment(id, page, 10);
		ArrayList<CommentAll> list = cdao.listVideoComment(id, page, 10000000);
		
//		//回复id，子回复数据
//		Map<Integer, ArrayList<CommentAll>> mmap0 = new HashMap<Integer, ArrayList<CommentAll>>();
//		
//		ArrayList<HashMap<String, Object>> outlist = new ArrayList<HashMap<String,Object>>();
//		
//		
//		for (CommentAll comm:list) {
//			ArrayList<CommentAll> sublist;
//			int recommid = comm.getT_comment_id();
//			if (recommid != -1) {
//				sublist = mmap0.get(recommid);
//				if (sublist==null) sublist = new ArrayList<CommentAll>();
//			}
//			
//		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		//发送的留言id,评论该留言列表
		Map<Integer, ArrayList<CommentAll>> mmap = new HashMap<Integer, ArrayList<CommentAll>>();
		
		LogUtil.print(JSONObject.toJSONString(list));
		
		for (CommentAll comment : list) {
			int commentid = comment.getT_comment_id();
			if (mmap.get(commentid)==null) {
				mmap.put(commentid, new ArrayList<CommentAll>());
			}
			
			int recommentid = comment.getT_comment_recommentid();
			if (recommentid != -1) {
				ArrayList<CommentAll> slist = mmap.get(recommentid);
				if (slist==null) {
					slist = new ArrayList<CommentAll>();
					mmap.put(recommentid, slist);
				}
				slist.add(comment);
			} else {
				
			}
		}		
		
		ArrayList<Map<String, Object>> out = new ArrayList<Map<String,Object>>();
		for (CommentAll comment : list) {
			if (comment.getT_comment_recommentid() == -1) {
				
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", comment.getT_comment_id());
				map.put("time", comment.getT_comment_addtime());
				map.put("videoid", comment.getT_comment_videoid());
				map.put("userid", comment.getT_comment_userid());
				map.put("username", comment.getT_user_truename());
				map.put("head", webUtil.getWebAbsoluteUrl(comment.getT_user_userhead(), Constant.PIC_userhead));
				map.put("content", comment.getT_comment_content());
				
				
				//子数据
				ArrayList<CommentAll> comms = mmap.get(comment.getT_comment_id());
				ArrayList<HashMap<String, Object>> subdata = new ArrayList<HashMap<String,Object>>();
				for (CommentAll comm:comms) {
					if (subdata.size()>5) {
						break;
					}
					
					HashMap<String, Object> submap = new HashMap<String, Object>();
					submap.put("addtime", comm.getT_comment_addtime());
					submap.put("content", comm.getT_comment_content());
					submap.put("courseid", comm.getT_comment_courseid());
					submap.put("id", comm.getT_comment_id());
					submap.put("isread", comm.getT_comment_isread());
					submap.put("recommentid", comm.getT_comment_recommentid());
					submap.put("sectionid", comm.getT_video_sectionid());
					submap.put("touserid", comm.getT_comment_touserid());
					submap.put("tousername", comm.getT_comment_tousername());
					submap.put("username", comm.getT_user_nick());
					submap.put("videoid", comm.getT_video_id());
					submap.put("head",webUtil.getWebAbsoluteUrl(comment.getT_user_userhead(), Constant.PIC_userhead));
					subdata.add(submap);
				}
				map.put("subdata",subdata );
				
				out.add(map);
			}
		}
		
		O.printSuccess(response, out);
	}

}
